package controllers

import (
	"HomeMaterialManagementSystem/thirdparty/tencentocr"
	"io"
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"
)

func apiBarcodeHandler(c *gin.Context) {
	content, err := io.ReadAll(c.Request.Body)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{})
		return
	}
	imageData := string(content)
	if !strings.HasPrefix(imageData, "data:image/") {
		c.JSON(http.StatusBadRequest, gin.H{})
		return
	}
	parts := strings.Split(imageData, ",")
	b64 := strings.TrimSpace(parts[1])
	barcodeInfo := tencentocr.GetBarcodeInfo(b64)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, barcodeInfo)
}
